package com.quasar.hpatient.module.doctor_search;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.doctor.DoctorBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.base.http.HttpResult;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.util.SpannableUtil;
import lib.quasar.widget.menu.DropDownMenu;
import okhttp3.RequestBody;

/**
 * description: 我的医生 ==> 添加医生
 * created by kalu on 2018/3/28 10:53
 */
public class DoctorSearchPresenter implements BasePresenter {

    private final ArrayList<DoctorBean> mDatas = new ArrayList<>();

    void initList(final DoctorSearchView view, RecyclerView recycler) {

        if (null == view || null == recycler) return;

        // 适配器
        final BaseCommonAdapter<DoctorBean> adapter = new BaseCommonAdapter<DoctorBean>(mDatas, R.layout.activity_doctor_search_child) {

            @Override
            protected void onNext(RecyclerHolder holder, DoctorBean model, int position) {

                final ImageView image = holder.getView(R.id.activity_doctor_search_child_head);
                view.setImage(image, model.getCover(), true);
                view.setText(holder, R.id.activity_doctor_search_child_name, model.getReal_name());
                view.setText(holder, R.id.activity_doctor_search_child_type, model.getPosition_name());
                view.setText(holder, R.id.activity_doctor_search_child_hospital, model.getHospital_name());
                SpannableUtil.append("动态：", BaseApp.getResource().getColor(R.color.color_bg_theme));
                SpannableUtil.append("发表文章《什么是器官移植呢？》发表文章《什么是器官移植呢？》");
                view.setText(holder, R.id.activity_doctor_search_child_dynamics, SpannableUtil.build());

                view.setText(holder, R.id.activity_doctor_search_child_apply, "申请");
                holder.setOnClickListener(R.id.activity_doctor_search_child_apply, v -> view.showChat(model.getDoctorid()));
            }
        };
        adapter.setNullView(BaseApp.getContext(), R.layout.layout_recycler_loading);
        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        view.setRecycler(recycler, adapter, manager);
    }

    void initMenu(final DoctorSearchView view, DropDownMenu menu) {

        if (null == view || null == menu) return;

        final List<String> args1 = Arrays.asList("医院", "科室");

        final List<String> strs1 = Arrays.asList("医院1", "医院2");
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(BaseApp.getContext(), android.R.layout.simple_list_item_1, strs1);
        final ListView list1 = new ListView(BaseApp.getContext());
        list1.setBackgroundColor(Color.WHITE);
        list1.setAdapter(adapter1);
        list1.setOnItemClickListener((parent, view1, position, id) -> menu.setTabText(strs1.get(position)));

        final List<String> strs2 = Arrays.asList("科室1", "科室2");
        ArrayAdapter<String> adapter2 = new ArrayAdapter<>(BaseApp.getContext(), android.R.layout.simple_list_item_1, strs2);
        final ListView list2 = new ListView(BaseApp.getContext());
        list2.setBackgroundColor(Color.WHITE);
        list2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        list2.setAdapter(adapter2);
        list2.setOnItemClickListener((parent, view1, position, id) -> menu.setTabText(strs2.get(position)));

        final List<View> args2 = Arrays.asList(list1, list2);

        View args3 = new View(BaseApp.getContext());
        args3.setBackgroundColor(Color.TRANSPARENT);
        args3.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        menu.setDropDownMenu(args1, args2, args3);
    }

    void doctorList(final DoctorSearchView view, RecyclerView recycler) {

        if (null == view) return;

        final RequestBody params = createParams(null,HttpParams.DATA_TYPE_1_5);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().doctorList(params);

        request(observable, new OnModelAcceptChangeListener<List<DoctorBean>>() {

            @Override
            public void modelFail() {
                view.setListNull(recycler, R.layout.layout_recycler_fail);
            }

            @Override
            public void modelSucc(List<DoctorBean> result) {

                if (null == result || result.size() == 0) {
                    modelFail();
                    return;
                }

                mDatas.addAll(result);
                view.refreshList(recycler);
            }
        });
    }

    void applyMessage(DoctorSearchView view, long doctorid) {

        final HashMap<String, Object> map = new HashMap<>();
        map.put(HttpParams.DOCTOR_ID, doctorid);

        map.put(HttpParams.RELATION_TYPE, String.valueOf(1));
        final RequestBody params = createParams(map, HttpParams.DATA_TYPE_16);

        final Observable<HttpResult<Object>> observable = HttpClient.getSingleHolder().getHttpService().applyFriend(params);
        request(observable, new OnModelAcceptChangeListener<Object>() {

            @Override
            public void modelFail() {
                view.toast("发送消息失败");
            }

            @Override
            public void modelSucc(Object result) {
                view.toast("发送消息成功");
            }
        });
    }

    @Override
    public void recycler() {
    }
}
